<?php

/**
 * Created by PhpStorm.
 * User: GadflyBSD
 * Date: 16/6/24
 * Time: 10:00
 */
class WebappAction extends CommonAction{
	public function _initialize(){
		parent::_initialize();
	}

	public function index(){
		header("Location: ".$this->weixin->get_auth2_url(APP_URL.U(MODULE_NAME.'/WxIndex')));
	}

	public function onePage(){
		if($this->checkWeixin()){
			switch(I('get.type')){
				case 'survey':
					$this->assign(M('WakaRealname')->where('rid='.I('get.rid'))->find());
					break;
				case 'test':
					dump($_SESSION);
					break;
			}
			$this->display(I('get.type'));
		}else{
			header("Location: ".$this->weixin->get_auth2_url(APP_URL.__SELF__, 'snsapi_userinfo'));
		}
	}

	public function onePagePostAjax($post){
		switch($post['op']){
			case 'setSurvey':
				return D('Customer')->setSurvey($post);
		}
	}

	/**
	 * 乐享哇咔微信 订阅行为响应和自动回复消息
	 * @throws Exception
	 */
	public function replay(){
		D('public')->replay('WKAPP', $this->sendNews());
	}

	/**
	 * 创建微信端自定义菜单
	 */
	public function setMenu(){
		header("Content-type: text/html; charset=utf-8");
		Vendor('Weixin');
		$weixin = new Weixin('WKAPP');
		$menu = array(
			'button'    => array(
				array(
					"type"  => "view",
					'name'  => '汪卡.发现',
					"url"   => APP_URL.U(MODULE_NAME.'/WxIndex'),
				),
				array(
					'name'      => '账户&账单',
					'sub_button'=> array(
						array(
							"type"  => "view",
							"name"  => "绑定微信",
							"url"   => APP_URL.U(MODULE_NAME.'/WxIndex', array('an' => 'bindWaka', 'px' => 'tab')),
						),
						array(
							"type"  => "view",
							"name"  => "绑定尽调员",
							"url"   => APP_URL.U(MODULE_NAME.'/WxIndex', array('an' => 'bindSurvey', 'px' => 'tab')),
						),
						array(
							"type"  => "view",
							"name"  => "个人信息",
							"url"   => APP_URL.U(MODULE_NAME.'/WxIndex', array('an' => 'userInfo', 'px' => 'tab')),
						),
						array(
							"type"  => "view",
							"name"  => "账单查询",
							"url"   => APP_URL.U(MODULE_NAME.'/WxIndex', array('an' => 'billSearch', 'px' => 'tab')),
						),
						array(
							"type"  => "view",
							"name"  => "消费查询",
							"url"   => APP_URL.U(MODULE_NAME.'/WxIndex', array('an' => 'consumeSearch', 'px' => 'tab')),
						),
					),
				),
				array(
					'name'      => '个人中心',
					'sub_button'=> array(
						array(
							"type"  => "view",
							"name"  => "帮助中心",
							"url"   => APP_URL.U(MODULE_NAME.'/WxIndex', array('an' => 'download', 'px' => 'tab')),
						),
						array(
							"type"  => "view",
							"name"  => "全安保障",
							"url"   => APP_URL.U(MODULE_NAME.'/WxIndex', array('an' => 'dkf', 'px' => 'tab')),
						),
						array(
							"type"  => "view",
							"name"  => "关于我们",
							"url"   => APP_URL.U(MODULE_NAME.'/WxIndex', array('an' => 'about', 'px' => 'tab')),
						),
						array(
							"type"  => "view",
							"name"  => "账户管理",
							"url"   => APP_URL.U(MODULE_NAME.'/WxIndex', array('an' => 'userinfo', 'px' => 'tab')),
						),
						array(
							"type"  => "view",
							"name"  => "系统设置",
							"url"   => APP_URL.U(MODULE_NAME.'/WxIndex', array('an' => 'about', 'px' => 'tab')),
						),
					),
				),
			)
		);
		dump($weixin->custom_button('create', $menu));
	}

	/**
	 * 关注和发信息给微信号时推送的新闻类图文消息的标题/说明/链接地址/图片等信息
	 * @param $param    所关注的微信号: Customer(乐享哇咔)/Business(哇咔商户, 默认)
	 *
	 * @return array
	 */
	private function sendNews(){
		return array(
			array(
				'Title'         => '您好！汪卡！',
				'Description'   => '汪卡APP安装地址...',
				'Url'           => 'http://www.wowcard.cn/download.html',
				'PicUrl'        => 'http://www.wowcard.cn/wechat/images/WechatBanner1.jpg',
			),
			array(
				'Title'         => '汪卡iOS端第一次运行操作说明',
				'Description'   => 'iPhone授权许可...',
				'Url'           => 'http://www.wowcard.cn/install_iOS.html',
				'PicUrl'        => 'http://www.wowcard.cn/wechat/images/WechatSecondBanner2.jpg',
			),
			array(
				'Title'         => '关于汪卡',
				'Description'   => '关于汪卡',
				'Url'           => 'http://www.wowcard.cn',
				'PicUrl'        => 'http://www.wowcard.cn/wechat/images/WechatSecondBanner3.jpg',
			),
		);
	}
}